package other

import (
	"data/config"
	"data/view/auth"
	"data/view/auth/auth_const"
	"data/view/user"
	"public/message"
	"public/mysqldb"
	"public/tools"

	"github.com/ant0ine/go-json-rest/rest"
)

// 编辑或新增省市区
func RegionAction(w rest.ResponseWriter, r *rest.Request) {
	var req RegionActionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Province) || req.Action == 0 {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_13, auth_const.L_A, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var region Region_config_tbl
	var err error
	if req.Action == 1 { //增加
		orm.Where("province = ? and city = ? and region = ?", req.Province, req.City, req.Region).Find(&region)
		if region.Id > 0 {
			w.WriteJson(message.GetErrorMsg(message.ExistedError))
			return
		}
		region.Province = req.Province
		region.City = req.City
		region.Region = req.Region
		err = orm.Create(&region).Error

	} else if req.Action == 2 { //修改
		sql := orm.Table("region_config_tbl")
		if len(req.City) == 0 {
			sql = sql.Where("province = ?", req.Province)
			orm.Where("province = ?", req.Province).Find(&region)
		} else {
			if len(req.Region) == 0 {
				sql = sql.Where("province = ? and city = ?", req.Province, req.City)
				orm.Where("province = ? and city = ?", req.Province, req.City).Find(&region)
			} else {
				sql = sql.Where("province = ? and city = ? and region = ?", req.Province, req.City, req.Region)
				orm.Where("province = ? and city = ? and region = ?", req.Province, req.City, req.Region).Find(&region)
			}
		}
		if region.Id == 0 {
			w.WriteJson(message.GetErrorMsg(message.NotFindError))
			return
		}
		var tmp Region_config_tbl
		tmp.Province = req.Province_new
		tmp.City = req.City_new
		tmp.Region = req.Region_new
		err = sql.Updates(tmp).Error
	} else {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid))
		return
	}

	if err != nil {
		w.WriteJson(message.GetErrorMsg(message.UnknownError))
		return
	}
	w.WriteJson(message.GetSuccessMsg(message.NormalMessageId))

}

//获取省市区
func GetRegion(w rest.ResponseWriter, r *rest.Request) {
	var req GetRegionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_13, auth_const.L_A, auth_const.L_B, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var result []*Region_info
	result = GetRegionLevel()
	msg := message.GetSuccessMsg(message.NormalMessageId)
	msg.Data = result
	w.WriteJson(msg)
}
